package com.infocollect.project.api.helper;


import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * jackjson的一些转换方法
 *
 */
public class JSONUtil {
    public static final ObjectMapper mapper = new ObjectMapper();

    private static final Logger logger = LoggerFactory.getLogger(JSONUtil.class);
        /*
         * 001.json转换成对象
         * @param:传入对象，json字符串
         * @return:Object
         */
        public static <T> T jsonToObj(Class<T> classType,String jsonStr)  {
            try {
            return mapper.readValue(jsonStr, classType);
            }catch (JsonParseException e){
                logger.error("json解析出错：" + jsonStr, e);
                return null;
            }catch (JsonMappingException e){
                logger.error("json解析出错：" + jsonStr, e);
                return null;
            }catch (IOException e) {
                logger.error("json解析出错：" + jsonStr, e);
                return null;
            }
        }
    /*
     * 001.json转换成对象
     * @param:传入对象，json字符串
     * @return:Object
     */
     static <T> T jsonToObj(JavaType javaType, String jsonStr)  {
      try{
           return (T)mapper.readValue(jsonStr,  javaType);
         }catch (JsonParseException e){
            logger.error("json解析出错：" + jsonStr, e);
            return null;
        }catch (JsonMappingException e){
            logger.error("json解析出错：" + jsonStr, e);
            return null;
        }catch (IOException e) {
            logger.error("json解析出错：" + jsonStr, e);
            return null;
        }
    }
    /*
     * 001.json转换成对象
     * @param:传入对象，json字符串
     * @return:Object
     */
   public  static <T> T jsonToObjList(Class classType,String jsonStr)  {
       try{
       JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, classType);
       return jsonToObj(javaType,jsonStr);
       } catch (Exception e) {
            logger.error("json解析出错：" + jsonStr, e);
            return null;
        }
   }
    /*
         * 002.对象转换成json
         * @param:传入对象
         * @return:json字符串
         */
        public static String objToJson(Object obj)  {
            if (obj == null) {
                return null;
            }
            if (obj.getClass() == String.class) {
                return (String) obj;
            }
            try {
            ObjectMapper mapper = new ObjectMapper();
            SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mapper.setDateFormat(smt);
          //  mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));//解决时区差8小时问题
            return mapper.writeValueAsString(obj);
            } catch (JsonProcessingException e) {
                logger.error("json序列化出错：" + obj, e);
                return null;
            }
        }


}
